package controllers

import (
	"cdam/models"
	"cdam/template"
	"cdam/util"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"io"
	"io/ioutil"
	"strings"
)

// AuthController operations for 用户权限部分
type GenController struct {
	beego.Controller
}

// URLMapping ...
func (c *GenController) URLMapping() {
	c.Mapping("Search", c.Search)
	c.Mapping("GenCode", c.GenCode)
	c.Mapping("GenCodeToJava",c.GenCodeToJava)
}



// SetAllJava ...
// @Title 根据表名生成代码
// @Description create Org
// @Param	body		body 	models.Org	true		"body for Org content"
// @Success 201 {int} models.Org
// @Failure 403 body is empty
// @router /GenCodeToJava [post]
func (c *GenController) GenCodeToJava()  {
	var v models.JavaCode
	var files []string
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		engine := template.InitParseJavaEngine()
		files,_ = engine.ParseInitCode(v)
	} else {
		println(err.Error())
	}
	dest :=  "./" + v.ClassName + ".zip"
	util.ZipFiles(dest,files,"./","")

	c.Ctx.Output.Header("Content-type", "application/force-download")
	c.Ctx.Output.Header("Content-Disposition", "attachment;filename= " + dest)
	c.Ctx.Output.Header("Pragma", "No-cache")
	c.Ctx.Output.Header("Cache-Control", "No-cache")
	c.Ctx.Output.Header("Expires", "0")

	r := strings.NewReader(string(readFile(dest)))
	io.Copy(c.Ctx.ResponseWriter,r)
}


// GetAll ...
// @Title 根据表名生成代码
// @Description get Org
// @Param   tablename		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Org
// @Failure 403
// @router /GenCode/:tablename [get]
func (c *GenController) GenCode() {
	tableNameStr := c.Ctx.Input.Param(":tablename")
	m := map[string]interface{}{}
	tool := template.InitTemplateEngine()
	result, err := models.GetTableDetail(tableNameStr)
	if err != nil {
		m["code"] = "80000"
		m["message"] = err.Error()
	} else {
		m["code"] = "20000"

		files, _ := tool.ParseToCode(result, tableNameStr)
		m["data"] = files
	}
	/* tableNameStr := c.Ctx.Input.Param(":tablename")
	m := map[string]interface{}{}
	tool := template.InitTemplateEngine()
    result ,err := models.GetTableDetail(tableNameStr)
	if err != nil {
		m["code"] = "80000"
		m["message"] = err.Error()
	}else{
		m["code"] = "20000"

		files,_ := tool.ParseToCode(result,tableNameStr)
>>>>>>> Stashed changes
		m["data"] = files
	}
	c.Data["json"] = m
	c.ServeJSON()
<<<<<<< Updated upstream

}

=======
	*/
	// c.Ctx.Output.Download("./lastupdate.tmp")
}

/*readFile 读取文件 | 参数说明：path 文件路径*/
func readFile(path string) []byte {
	b, err := ioutil.ReadFile(path) // just pass the file name
	if err != nil {
		fmt.Print(err)

	}
	return b
}

// GetAll ...
// @Title 查询所有table
// @Description get Org
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Success 200 {object} models.Org
// @Failure 403
// @router / [get]
func (c *GenController) Search() {
	m := map[string]interface{}{}
	tables, err := models.GetAllTable()
	if err != nil {
		m["code"] = "80000"
		m["message"] = err.Error()
	}else{
		m["code"] = "20000"
		m["data"] = tables
	}
	c.Data["json"] = m
	c.ServeJSON()
}